package com.youlu.campus.admin.university;

import com.google.common.collect.Lists;
import com.youlu.campus.admin.utils.ExcelUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.UniversityInfo;
import com.youlu.campus.entity.VO.req.ImportUniversityExcelVO;
import com.youlu.campus.entity.VO.req.ImportUniversityInfoExcelVO;
import com.youlu.campus.entity.VO.req.UniversityInfoQueryVO;
import com.youlu.campus.service.university.UniversityInfoService;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 大学管理
 *
 * @Description: UniversityInfoController
 * @Date 2025/7/2
 * @author: su
 */
@RestController
public class UniversityInfoController {
    @Autowired
    private UniversityInfoService universityInfoService;

    /**
     * 分页查询大学列表
     *
     * @param request
     * @return
     */
    @PostMapping("/university/list")
    public QResult list(@NotNull @RequestBody UniversityInfoQueryVO request) {
        Page<UniversityInfo> datas = universityInfoService.list(request);
        return QResult.success(datas);
    }

    /**
     * 根据ids 查询大学列表
     *
     * @param request
     * @return
     */
    @PostMapping("/university/find-list")
    public QResult listByIds(@NotNull @RequestBody UniversityInfoQueryVO request) {
        if (CollectionUtils.isEmpty(request.getIds())) {
            return QResult.success(Lists.newArrayList());
        }
        List<UniversityInfo> datas = universityInfoService.listByIds(request.getIds());
        return QResult.success(datas);
    }

    /**
     * 创建大学
     *
     * @param request
     * @return
     */
    @PostMapping("/university/create")
    public QResult create(@NotNull @RequestBody UniversityInfo request) {
        boolean result = universityInfoService.create(request);
        if (result) {
            return QResult.success(result);
        }
        return QResult.fail("创建大学信息失败");
    }

    /**
     * 删除大学
     *
     * @param id
     * @return
     */
    @GetMapping("/university/delete")
    public QResult delete(@NotNull @RequestParam("id") String id) {
        boolean result = universityInfoService.delete(id);
        if (result) {
            return QResult.success(result);
        }
        return QResult.fail("删除大学信息错误");
    }

    /**
     * 修改大学
     *
     * @param request
     * @return
     */
    @PostMapping("/university/update")
    public QResult update(@NotNull @RequestBody UniversityInfo request) {
        boolean result = universityInfoService.update(request);
        if (result) {
            return QResult.success(result);
        }
        return QResult.fail("修改大学信息错误");
    }

    /**
     * 根据id查询大学
     *
     * @param id
     * @return
     */
    @GetMapping("/university/find-one")
    public QResult findOne(@NotNull @RequestParam("id") String id) {
        return QResult.success(universityInfoService.findOne(id));
    }

    /**
     * 大学批量导入
     *
     * @param file
     * @return
     */
    @PostMapping("/public/university/import")
    public RestResult importUniversity(@RequestParam("file") MultipartFile file) {
        List<ImportUniversityExcelVO> excelDataList = ExcelUtils.readExcel(file, ImportUniversityExcelVO.class);
        if (CollectionUtils.isEmpty(excelDataList)) {
            return RestResultBuilder.builder().failure("导入文件错误").build();
        }
        universityInfoService.importUniversity(excelDataList);
        return RestResultBuilder.builder().success(true).build();
    }

    /**
     * 大学批量导入
     *
     * @param file
     * @return
     */
    @PostMapping("/university/batch/import")
    public RestResult importUniversityInfo(@RequestParam("file") MultipartFile file) {
        List<ImportUniversityInfoExcelVO> excelDataList = ExcelUtils.readExcel(file, ImportUniversityInfoExcelVO.class);
        if (CollectionUtils.isEmpty(excelDataList)) {
            return RestResultBuilder.builder().failure("导入文件错误").build();
        }
        universityInfoService.importBatchUniversity(excelDataList);
        return RestResultBuilder.builder().success(true).build();
    }

    /**
     * 物理删除大学
     *
     * @param id
     * @return
     */
    @GetMapping("/university/delete-physics")
    public QResult deletePshysics(@NotNull @RequestParam("id") String id) {
        boolean result = universityInfoService.deletePshysics(id);
        if (result) {
            return QResult.success(result);
        }
        return QResult.fail("物理删除大学信息错误");
    }


}
